package io.gitee.yxsnake.embark.web.core.framework.controller;

import com.google.common.collect.Maps;
import io.gitee.yxsnake.embark.web.core.assertion.Assert;
import io.gitee.yxsnake.embark.web.core.base.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.date.DateUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @author: snake
 * @create-time: 2024-09-29
 * @description:
 * @version: 1.0
 */
@Tag(name = "服务健康检查")
@Slf4j
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ConditionalOnProperty(prefix = "web-core.health", name = "enabled", havingValue = "true", matchIfMissing = true)
public class HealthController{

    @Resource
    private Environment environment;

    public HealthController(){
        log.info("健康检查接口已加载.....................");
    }

    @Operation(summary = "健康检查接口")
    @GetMapping("${web-core.health.path:/health}")
    public ResponseEntity<Result> health() {
        // 获取应用名称
        String applicationName = environment.getProperty("spring.application.name");
        Assert.nonNull("Configure the value of the spring.application.name configuration item",applicationName);
        Map<String,Object> data = Maps.newHashMap();
        data.put("applicationName",applicationName);
        data.put("currentDate",DateUtil.now());
        return ResponseEntity.ok(Result.success(data));
    }
}
